package cluster_node.serialization;

import com.eclipsesource.json.JsonArray;
import com.eclipsesource.json.JsonObject;
import org.eclipse.leshan.core.model.LwM2mModel;
import org.eclipse.leshan.core.model.ObjectModel;
import org.eclipse.leshan.core.model.ResourceModel;

public class ModelSerializer {

    public static JsonObject serializeToJson(LwM2mModel lwM2mModel) {
        JsonObject result = new JsonObject();

        JsonArray objs = new JsonArray();

        for (ObjectModel objectModel: lwM2mModel.getObjectModels()) {

            JsonObject object = new JsonObject();
            object.add("objectId", objectModel.id);
            object.add("name", objectModel.name);
            object.add("mandatory", objectModel.mandatory);

            JsonArray resources = new JsonArray();
            for (ResourceModel resourceModel : objectModel.resources.values()) {
                JsonObject resource = new JsonObject();
                resource.add("id", resourceModel.id);
                resource.add("name", resourceModel.name);
                resource.add("operations", resourceModel.operations.name());
                resource.add("type", resourceModel.type.name());

                resources.add(resource);
            }
            object.add("resources", resources);

            objs.add(object);
        }
        result.add("objs", objs);

        return result;
    }
}
